import fetch from 'assets/js/fetch'
import { AJAX_SERVICE_HOST, SITE_PATH, getAppVersion } from 'api/config'
import { getToken, getUserInfo } from 'assets/js/cache'

export function wxLogin() {
  Indicator.open()
  location.href = `${SITE_PATH}wxLogin.html?v=${getAppVersion()}`
}

export function login(params) {
  const url = AJAX_SERVICE_HOST + 'user/login/weixin/code2token'
  var res = fetch.get(url, params)
  return res
}

export function getSms(params) {
  const url = AJAX_SERVICE_HOST + 'account/login/sms'
  return fetch.get(url, params)
}

export function verifyToken() {
  let token = getToken()
  return !!token
}

export function verifyBaseInfo() {
  let userInfo = getUserInfo()
  if (!userInfo) {
    return false
  }
  return userInfo.areaName && userInfo.branchName
}

export function verifyVIP() {
  let userInfo = getUserInfo()
  if (!userInfo) {
    return false
  }
  return userInfo.vipFlag !== 0 && userInfo.vipExpiresIn.toDate() > (new Date())
}

export function logout() {
  const url = AJAX_SERVICE_HOST + 'account/logout'
  return fetch.get(url)
}

/**
 * 获取账户信息
 * @export
 * @returns
 */
export function getAccountInfo() {
  const url = AJAX_SERVICE_HOST + 'account/info'
  return fetch.get(url)
}
